/**
 * 
 */
package com.morningstar.qs.api.basic.common.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;


/**
 * @author rzeng
 * @deprecated this class is deprecated, please use om.mstar.qs.common.jdbc.JDBCManager instead. 
 */
public class ConnManager {
	private static Logger gLogger = Logger.getLogger(ConnManager.class);
	
	private ConnInfo connInfo = null;
	
	public ConnInfo getConnInfo() {
		return connInfo;
	}
	
	public ConnManager(ConnInfo info) {
		this.connInfo = info;
	}
	
	public Connection getConnection(DbType dbType) {
		Connection conn = null;
		
		switch (dbType) {
		case MYSQL:
			try {
				Class.forName("com.mysql.jdbc.Driver");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				gLogger.error("", e);
			}
			break;
		case MSSQL:
			try {
				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				gLogger.error("", e);
			}
			break;			
		default:
			gLogger.error("it is failed to register database driver.");
			return null;
		}
		
		if (null == this.connInfo) {
			gLogger.error("The value of connInfo object is null.");
			return null;
		}
		
		try {
			conn = DriverManager.getConnection(this.connInfo.getConnUrl(dbType), this.connInfo.getUserName(), this.connInfo.getUserPwd());
		} catch (SQLException e) {
			e.printStackTrace();
			gLogger.error("", e);
		}
		
		return conn;
	}
}
